//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Xml;

namespace CommunityServer.Components
{
	/// <summary>
	/// Summary description for AdConfiguration.
	/// </summary>
	public class AdsConfiguration : ApplicationConfiguration
	{
		public AdsConfiguration()
		{
		}

        private static readonly Type thisType = typeof(AdsConfiguration);

        public static AdsConfiguration Instance()
        {
            return Instance(true);
        }

        public static AdsConfiguration Instance(bool useCache)
        {
            return Instance(thisType,useCache) as AdsConfiguration;
        }

        private string[] _roles;
        public string[] Roles
        {
            get{return _roles;}
            set{_roles = value;}
        }

        protected override void Initialize(System.Xml.XmlNode node)
        {
            base.Initialize (node);
            if(node != null)
            {
                XmlAttribute roles = node.Attributes["roles"];
                if(roles != null)
                    this.Roles = roles.Value.Split(';');

                XmlAttribute inline = node.Attributes["children"];
                if(inline != null)
                    this.AllowInlineControl = bool.Parse(inline.Value);
            }
        }

        private bool _allowInLine = false;
        public bool AllowInlineControl
        {
            get{return _allowInLine;}
            set{_allowInLine = value;}
        }

	    protected override string ConfigSection
	    {
	        get { return "CommunityServer/Ads"; }
	    }

	    public override ApplicationType ApplicationType
	    {
	        get { return ApplicationType.Ads; }
	    }

        public static bool DisplayAd()
        {
            return DisplayAd(CSContext.Current.User);
        }

        public static bool DisplayAd(User user)
        {
            AdsConfiguration config = AdsConfiguration.Instance();
            if(!config.Enabled)
                return false;

            if(config.Roles == null || config.Roles.Length == 0)
                return true;

            return !user.IsInRoles(config.Roles);
        }


	}
}
